package utils;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import javax.swing.ImageIcon;


public class ImageUtils
{
    /**
     * resize an Image if it's superior than width and height <br>
     * contains proportions
     * 
     * @param image
     *            : Image to resize
     * @param width
     *            :
     * @param height
     * @return a final Image
     */
    public static Image resizeInferiorTo(Image image, int width, int height)
    {
        ImageIcon imgTmp = new ImageIcon(image);
        if (imgTmp.getIconHeight() > height)
        {
            float coef = (float) imgTmp.getIconHeight() / (float) height;
            int newWidth = (int) (imgTmp.getIconWidth() / coef);
            Image imgResize = imgTmp.getImage().getScaledInstance(
                    (newWidth == 0) ? 1 : newWidth, height, 0);
            imgTmp = new ImageIcon(imgResize);
        }

        if (imgTmp.getIconWidth() > width)
        {
            float coef = (float) imgTmp.getIconWidth() / (float) width;
            int newHeight = (int) (imgTmp.getIconHeight() / coef);
            Image imgResize = imgTmp.getImage().getScaledInstance(width,
                    (newHeight == 0) ? 1 : newHeight, 0);
            imgTmp = new ImageIcon(imgResize);
        }

        return imgTmp.getImage();
    }

    /**
     * convert a buffered image to an image
     * 
     * @param image
     *            : the buffered image to convert
     * @return the image
     */
    public static Image toImage(BufferedImage image)
    {
        ImageIcon img = new ImageIcon();
        img.setImage(image);
        return img.getImage();
    }

    /**
     * convert to a buffered image
     * 
     * @param image
     * @return a bufferedImage
     */
    public static BufferedImage toBufferedImage(Image image)
    {
        image = new ImageIcon(image).getImage();
        BufferedImage myBufferedImage = new BufferedImage(image.getWidth(null),
                image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
        Graphics g = myBufferedImage.createGraphics();
        g.drawImage(image, 0, 0, null);
        g.dispose();
        return (myBufferedImage);
    }

    public static BufferedImage deepCopy(BufferedImage bi)
    {
        ColorModel cm = bi.getColorModel();
        boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
        WritableRaster raster = bi.copyData(null);
        return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
    }

    public static BufferedImage fillTransparentPixels(BufferedImage image,
            Color fillColor)
    {
        int w = image.getWidth();
        int h = image.getHeight();
        BufferedImage image2 = new BufferedImage(w, h,
                BufferedImage.TYPE_INT_RGB);
        Graphics2D g = image2.createGraphics();
        g.setColor(fillColor);
        g.fillRect(0, 0, w, h);
        g.drawRenderedImage(image, null);
        g.dispose();
        return image2;
    }

    /**
     * resize a bufferedImage
     * 
     * @param m_image
     *            : source Image
     * @param width
     *            : width to resize
     * @param height
     *            : height to resize
     * @return the final buffered image
     */
    public static BufferedImage resizeTo(BufferedImage m_image, int width,
            int height)
    {
        ImageIcon imgIconTmp = new ImageIcon(m_image);
        Image imgTmp = imgIconTmp.getImage();
        try
        {
            imgTmp = imgTmp.getScaledInstance(width, height, 0);
        }
        catch (Exception e)
        {
            System.err.println("empty image");
        }
        return toBufferedImage(imgTmp);
    }

    public static int alphaToInt(int alpha)
    {
        return (alpha * 100) / 255;
    }
}
